package entity.connector;

import entity.bean.Movie;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Lusinda
 */
public class MovieDownloader {
    private int noOfMovies;
    private Movie[] movies;
    
    public MovieDownloader(boolean filter){
        try {
            Connection con = ConnectionProvider.getCon();
            PreparedStatement ps =
                    con.prepareStatement("SELECT * FROM movie WHERE (? = 0 OR "
                    + "movie_id IN (SELECT DISTINCT movie_id FROM movie_show)) ORDER BY movie_id",
                    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setBoolean(1, filter);
            ResultSet rs = ps.executeQuery();
            // find the number of movies
            rs.last();
            noOfMovies = rs.getRow();
            movies = new Movie[noOfMovies];
            // store data from result set to static data members
            rs.beforeFirst();
            for (int i = 0; rs.next() != false; i++) {
                movies[i] = new Movie(rs.getInt("movie_id"));
                movies[i].setPrice(rs.getInt("default_price"));
                movies[i].setName(rs.getString("movie_name"));
                movies[i].setDirector(rs.getString("director"));
                movies[i].setCast(rs.getString("cast"));
                movies[i].setDuration(rs.getInt("duration"));
                movies[i].setCategary(rs.getString("categary"));
                movies[i].setLanguage(rs.getString("language"));
                movies[i].setSynopsis(rs.getString("synopsis"));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
        }
    }
    
    public int getNoOfMovies(){
        return noOfMovies;
    }
    
    public Movie[] getMovies(){
        return movies;
    }
}
